﻿function AutoResizer(element) {
    if (!element) return;

    this.UI = new Object();
    this.UI.Element = $(element);
    this.UI.Parent = this.UI.Element.parent();

    this.UI.Element.data('controller', this);

    this.AdjustWidth = parseInt(this.UI.Element.attr('data-adjustwidth'));
    if (!this.AdjustWidth)
        this.AdjustWidth = 0;
};

AutoResizer.prototype.Resize = function () {
    var othersWidth = 0;
    this.UI.Parent.children().each(function () {
        if (!$(this).is('*[data-type=fill-space]')) {
            othersWidth += $(this).outerWidth();
        }
    });
    this.UI.Element.width((this.UI.Parent.width() - othersWidth) + this.AdjustWidth);
};

AutoResizer.prototype.Init = function () {
    $('*[data-type=fill-space]').each(function () {
        var ar = new AutoResizer(this);
    });
};
AutoResizer.prototype.Load = function () {
    $('*[data-type=fill-space]').each(function () {
        $(this).data('controller').Resize();
    });
    $(window).resize(function () {
        window.Nemo.AutoResizer.Load();
    });
};
var autoresizer = new AutoResizer();
window.Nemo.RegisterController(autoresizer);
window.Nemo.AutoResizer = autoresizer;